﻿using System;
using System.Collections.Generic;
using System.Text;
using ADOX;
using System.Data.Common;
using System.Data;
using System.Data.OleDb;

namespace ImbedLibrary
{
    public class Access
    {
        private string connString { get; set; }

        private OleDbConnection conn = null;

        public Access(string connString)
        {
            this.connString = connString;
        }

        /// <summary>
        /// 更改数据库表中的字段名称
        /// </summary>
        /// <param name="tablename">表名</param>
        /// <param name="oldname">原字段名称</param>
        /// <param name="newname">新字段名称</param>
        public void AlterColumnName(string tablename, string oldname, string newname)
        {
            Catalog catalog = new Catalog();
            catalog.let_ActiveConnection(connString);
            try
            {
                Table table = catalog.Tables[tablename];
                Column column = table.Columns[oldname];
                column.Name = newname;
                conn = catalog.ActiveConnection as OleDbConnection;
            }
            catch (Exception e)
            {
                if (e.Message != "异常来自 HRESULT:0x800A0CC1")
                {
                    throw;
                }
            }
            finally
            {
                Dispose(catalog);
            }
        }

        /// <summary>
        /// 销毁资源
        /// </summary>
        /// <param name="catalog"></param>
        private static void Dispose(Catalog catalog)
        {
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(catalog.ActiveConnection);
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(catalog);
        }
    }
}
